package com.kma.summer2012.client.presenters.manager;


import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.DialogBoxFactory;
import com.kma.summer2012.client.events.manager.AddToolEvent;
import com.kma.summer2012.client.events.request.MainPageEvent;
import com.kma.summer2012.client.presenters.Presenter;
import com.kma.summer2012.client.services.AbstractCallBack;
import com.kma.summer2012.client.services.CrumbsCatalogOfTools.CrumbsCatalogOfToolsService;
import com.kma.summer2012.client.view.PopUpPreloader;
import com.kma.summer2012.shared.model.UICrumbsToolItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 21.02.13
 * Time: 23:35
 * To change this template use File | Settings | File Templates.
 */
public class ManageToolsPresenter implements Presenter {
    public interface Display {
        void setData(ArrayList<UICrumbsToolItem> toolItems);

        public void setEnableDel();

        public void setDisableDel();

        public void setEnableEdit(boolean a);

        HasClickHandlers getDeleteToolButton();

        HasClickHandlers getBackButton();

        HasClickHandlers getAddToolButton();

        DataGrid<UICrumbsToolItem> getTable();

        public HasClickHandlers getEditToolButton();

        Widget asWidget();

        MultiSelectionModel<UICrumbsToolItem> getSelectionModel();

        void setSelectionModel(MultiSelectionModel<UICrumbsToolItem> selectionModel);

        void setDoubleHandler(DoubleClickHandler handler);

        Set<UICrumbsToolItem> getSelectedTools();
    }

    private final Display display;
    private List<UICrumbsToolItem> selectedTools;

    public ManageToolsPresenter(Display display) {
        this.display = display;
        bind();
        CrumbsCatalogOfToolsService.App.getInstance().getAllTools(new AbstractCallBack<List<UICrumbsToolItem>>() {
           @Override
            public void onSuccess(List<UICrumbsToolItem> uiCrumbsToolItems) {
                ManageToolsPresenter.this.display.setData((ArrayList<UICrumbsToolItem>) uiCrumbsToolItems);
            }
        });


    }

    private void bind() {
        this.display.getAddToolButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doAddTool();
            }
        });
        this.display.getDeleteToolButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                List<HasClickHandlers> list = new ArrayList<HasClickHandlers>();
                list.add(new Button("Delete", new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent clickEvent) {
                        doDelete();//To change body of implemented methods use File | Settings | File Templates.
                    }
                }));
                DialogBoxFactory.createMessageBox("Do you really want to delete this tool?", "O'RLY?", list).show();
            }
        });
        this.display.getBackButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                CrumbsEventBus.getInstance().fireEvent(new MainPageEvent());
            }
        });
        this.display.getEditToolButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                List<UICrumbsToolItem> list = ManageToolsPresenter.this.selectedTools;
                if (list.size() == 1) {
                    CrumbsEventBus.getInstance().fireEvent(new AddToolEvent(list.get(0)));
                }
            }
        });

        addSelectionModel();
    }


    private void doDelete() {
        //  ArrayList<UICrumbsToolItem> selectedTools = this.display.getTable().getSelectionModel()
        // Set<UICrumbsToolItem> selectedTools = this.display.getSelectedTools();
        PopUpPreloader.show();
        CrumbsCatalogOfToolsService.App.getInstance().deleteAllTool(selectedTools, new AbstractCallBack<Void>() {
           @Override
            public void onSuccess(Void aVoid) {
                ManageToolsPresenter.this.display.setDisableDel();
                CrumbsCatalogOfToolsService.App.getInstance().getAllTools(new AbstractCallBack<List<UICrumbsToolItem>>() {
                    @Override
                    public void onSuccess(List<UICrumbsToolItem> uiCrumbsToolItems) {
                        PopUpPreloader.hide();
                        ManageToolsPresenter.this.display.setData((ArrayList<UICrumbsToolItem>) uiCrumbsToolItems);
                    }
                });
            }
        });

    }

    private void addSelectionModel() {
        final MultiSelectionModel<UICrumbsToolItem> selectionModel = new MultiSelectionModel<UICrumbsToolItem>();
        DoubleClickHandler handler = new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                Set<UICrumbsToolItem> set = selectionModel.getSelectedSet();
                if (set.size() == 1) {
                    CrumbsEventBus.getInstance().fireEvent(new AddToolEvent((UICrumbsToolItem) set.toArray()[0]));
                }
            }
        };
        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
            @Override
            public void onSelectionChange(SelectionChangeEvent selectionChangeEvent) {
                Set<UICrumbsToolItem> set = ((MultiSelectionModel) selectionChangeEvent.getSource()).getSelectedSet();
                List<UICrumbsToolItem> list = new ArrayList<UICrumbsToolItem>();
                list.addAll(set);
                selectedTools = list;
                if (list.size() > 0) {
                    if (list.size() == 1) {
                        ManageToolsPresenter.this.display.setEnableEdit(true);
                    } else {
                        ManageToolsPresenter.this.display.setEnableEdit(false);
                    }
                    ManageToolsPresenter.this.display.setEnableDel();

                } else {
                    ManageToolsPresenter.this.display.setEnableEdit(false);
                    ManageToolsPresenter.this.display.setDisableDel();
                }
            }
        });
        this.display.setSelectionModel((selectionModel));
        this.display.getTable().addDomHandler(handler, DoubleClickEvent.getType());
      /*
        this.display.getTable().addHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                Element cell = doubleClickEvent.getNativeEvent().getEventTarget().cast();
                //     doEditTool(doubleClickEvent.getClass().get);//To change body of implemented methods use File | Settings | File Templates.
            }
        },DoubleClickEvent.getType());
        */
    }

    private void doAddTool() {
        UICrumbsToolItem uiTool = new UICrumbsToolItem();

        uiTool.setId(0);
        //uiTool.setToolName("Edit me!");
        CrumbsEventBus.getInstance().fireEvent(new AddToolEvent(uiTool));
    }

    private void doEditTool(UICrumbsToolItem uiTool) {
        CrumbsEventBus.getInstance().fireEvent(new AddToolEvent(uiTool));
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());

    }


}
